
namespace 背单词
{
    using MySql.Data.MySqlClient;
    using System;
    using System.Data;

    public partial class Form1 : Form
    {
        int counter = 0;
        string[] chinese = new string[100];
        string[] english = new string[100];
        int totalWords = 0;
        MySqlConnection words;
        public Form1()
        {
            InitializeComponent();
            
        }
        private void LoadWords()
        {
            try
            {
                words.Open();
                string query = "SELECT 英文, 中文 FROM word";  
                MySqlCommand cmd = new MySqlCommand(query, words);
                MySqlDataReader reader = cmd.ExecuteReader();

                int index = 0;
                while (reader.Read() && index < 100)
                {
                    english[index] = reader["英文"].ToString();
                    chinese[index] = reader["中文"].ToString();
                    index++;
                }
                totalWords = index;  // 记录实际加载的单词数量
                reader.Close();

                if (totalWords > 0)
                {
                    ShowCurrentWord();  // 显示第一个单词
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据库错误: {ex.Message}");
            }
            finally
            {
                if (words.State == ConnectionState.Open)
                    words.Close();
            }
        }

        private void ShowCurrentWord()
        {
            Chinese_word.Text = chinese[counter];
            judgeLabel.Text = "";
            wordBox.Clear();
        }
        private void left_button_Click(object sender, EventArgs e)
        {
            if (counter > 0)
            {
                counter--;
                ShowCurrentWord();
            }
            else
            {
                MessageBox.Show("已经是第一个单词！");
            }
        }

        private void right_button_Click(object sender, EventArgs e)
        {
            if (counter < totalWords - 1) // 确保索引不超过 totalWords-1
            {
                counter++;
                ShowCurrentWord();
            }
            else
            {
                MessageBox.Show("已经是最后一个单词！");
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string userInput = wordBox.Text.Trim();
                string correctAnswer = english[counter];

                if (string.Equals(userInput, correctAnswer, StringComparison.OrdinalIgnoreCase))
                {
                    judgeLabel.Text = "正确！";
                    judgeLabel.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    judgeLabel.Text = $"错误！正确答案：{correctAnswer}";
                    judgeLabel.ForeColor = System.Drawing.Color.Red;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionString = "Server=localhost;Database=word;Uid=root;Pwd=123456;Port=3306;";
            words = new MySqlConnection(connectionString);
            LoadWords();  // 加载单词数据
        }
    }
}
